/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Controllers;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.RequestDispatcher;
import java.util.Vector;

import Models.TienTe_Model;
import Models.NhatKyHT_Model;

import Beans.NhanVien;

/**
 *
 * @author Venus
 */
public class TienTe_Controller extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();
        try {
            if (session.getAttribute("langid") == null) {
                response.sendRedirect("index.jsp");
                return;
            }
            session.removeAttribute("DSMA");
            session.removeAttribute("DSNMA");
            session.removeAttribute("item");
            String action = request.getParameter("action");
            int langid = (Integer) session.getAttribute("langid");
            if (action.equals("Show")) { // Hiển thị trang quản lý khu vực
                String item = request.getParameter("item"); // trang hiện tại - phân trang
                TienTe_Model ttm = new TienTe_Model();
                Vector v = new Vector();
                v = ttm.LayDSTT(langid); // Lấy danh sách khu vực
                request.setAttribute("DSTT", v);
                request.setAttribute("item", item);
                RequestDispatcher rd = request.getRequestDispatcher("admin/newcurrency.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("Add")) {   
                String currencycode = request.getParameter("currencycode");
                String currencyname = request.getParameter("currencyname");
                String symbols = request.getParameter("symbols");                
                if (TienTe_Model.KiemTraTenTT(currencycode)) {
                    out.print("currencyexist"); // khu vực đã tồn tại
                } else {                                     
                    TienTe_Model.AddTienTe(currencycode, currencyname, symbols);
                    NhanVien nv = new NhanVien();
                    nv = (NhanVien) session.getAttribute("nv");
                    NhatKyHT_Model.GhiNhatKy(2, nv.getMaNV(), "currency");
                    TienTe_Model ttm = new TienTe_Model();
                    Vector v = new Vector();
                    v = ttm.Detail(currencycode);
                    request.setAttribute("Detail", v);
                    RequestDispatcher rd = request.getRequestDispatcher("admin/currencylang.jsp");
                    rd.forward(request, response);
                }
            }
            else if (action.equals("ShowCurrency")) {
                String item = request.getParameter("item");
                TienTe_Model ttm = new TienTe_Model();
                Vector v = new Vector();
                v = ttm.LayDSTT(langid); 
                request.setAttribute("DSTT", v);
                request.setAttribute("item", item);
                RequestDispatcher rd = request.getRequestDispatcher("admin/listcurrency.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("SortTenTT")) {   // Sort tên khu vực
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                TienTe_Model ttm = new TienTe_Model();
                Vector v = new Vector();
                v = ttm.SortTenTT(langid, order);
                request.setAttribute("DSTT", v);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "1");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortcurrency.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("SortKyHieuTT")) {  
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                TienTe_Model ttm = new TienTe_Model();
                Vector v = new Vector();
                v = ttm.SortKyHieuTT(langid, order);
                request.setAttribute("DSTT", v);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "2");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortcurrency.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("SortTTTT")) {
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                TienTe_Model ttm = new TienTe_Model();
                Vector v = new Vector();
                v = ttm.SortTTTT(langid, order);
                request.setAttribute("DSTT", v);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "3");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortcurrency.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("Search")) {
                String tentt = request.getParameter("tentt");
                String kyhieu = request.getParameter("kyhieu");
                String macdinh = request.getParameter("macdinh");
                String item = request.getParameter("item");
                TienTe_Model ttm = new TienTe_Model();
                Vector v = new Vector();
                v = ttm.Search(tentt, kyhieu, macdinh,  langid);
                request.setAttribute("kyhieu", kyhieu);
                request.setAttribute("tentt", tentt);
                request.setAttribute("macdinh", macdinh);
                request.setAttribute("item", item);
                request.setAttribute("DSTT", v);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssearchcurrency.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("SortSearchTenTT")) {
                String tentt = request.getParameter("tentt");
                String kyhieu = request.getParameter("kyhieu");
                String macdinh = request.getParameter("macdinh");
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                TienTe_Model ttm = new TienTe_Model();
                Vector v = new Vector();
                v = ttm.SortSearchTenTT(tentt, kyhieu, macdinh, langid, order);
                request.setAttribute("DSTT", v);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "1");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                request.setAttribute("kyhieu", kyhieu);
                request.setAttribute("tentt", tentt);
                request.setAttribute("macdinh", macdinh);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortsearchcurrency.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("SortSearchKyHieuTT")) {
                String tentt = request.getParameter("tentt");
                String kyhieu = request.getParameter("kyhieu");
                String macdinh = request.getParameter("macdinh");
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                TienTe_Model ttm = new TienTe_Model();
                Vector v = new Vector();
                v = ttm.SortSearchKyHieuTT(tentt, kyhieu, macdinh, langid, order);
                request.setAttribute("DSTT", v);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "2");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                request.setAttribute("kyhieu", kyhieu);
                request.setAttribute("tentt", tentt);
                request.setAttribute("macdinh", macdinh);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortsearchcurrency.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("SortSearchTTTT")) {
                String tentt = request.getParameter("tentt");
                String kyhieu = request.getParameter("kyhieu");
                String macdinh = request.getParameter("macdinh");
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                TienTe_Model ttm = new TienTe_Model();
                Vector v = new Vector();
                v = ttm.SortSearchTTTT(tentt, kyhieu, macdinh, langid, order);
                request.setAttribute("DSTT", v);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "3");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                request.setAttribute("kyhieu", kyhieu);
                request.setAttribute("tentt", tentt);
                request.setAttribute("macdinh", macdinh);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortsearchcurrency.jsp");
                rd.forward(request, response);
            }            
            else if (action.equals("DeleteTT")) {
                String matte = request.getParameter("matte");
                String item = request.getParameter("item");
                String MaTienTe[] = matte.split("-");
                int j = 0;
                while (j < MaTienTe.length) {
                    TienTe_Model.Delete(MaTienTe[j]);
                    j = j + 1;
                }
                TienTe_Model ttm = new TienTe_Model();
                Vector v = new Vector();
                v = ttm.LayDSTT(langid); // Lấy danh sách khu vực
                NhanVien nv = new NhanVien();
                nv = (NhanVien) session.getAttribute("nv");
                NhatKyHT_Model.GhiNhatKy(4, nv.getMaNV(), "currency");
                request.setAttribute("DSTT", v);
                request.setAttribute("item", item);
                RequestDispatcher rd = request.getRequestDispatcher("admin/listcurrency.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("Edit")) {
                String matiente = request.getParameter("matiente");                
                String TenTTe = request.getParameter("TenTTe");
                String KyHieu = request.getParameter("KyHieu");                
                String mann = request.getParameter("mann");
                TienTe_Model.Edit(matiente,  TenTTe, KyHieu,  mann);
                NhanVien nv = new NhanVien();
                nv = (NhanVien) session.getAttribute("nv");
                NhatKyHT_Model.GhiNhatKy(3, nv.getMaNV(), "currencyname");
                TienTe_Model ttm = new TienTe_Model();
                Vector v = new Vector();
                v = ttm.Detail(matiente);
                request.setAttribute("Detail", v);
                RequestDispatcher rd = request.getRequestDispatcher("admin/currencylang.jsp");
                rd.forward(request, response);                
            }
            else if (action.equals("ShowLangTT")) {
                String MaTTe = request.getParameter("MaTTe");
                TienTe_Model ttm = new TienTe_Model();
                Vector v = new Vector();
                v = ttm.Detail(MaTTe);
                request.setAttribute("Detail", v);
                RequestDispatcher rd = request.getRequestDispatcher("admin/currencylang.jsp");
                rd.forward(request, response);
            }
            else if(action.equals("CheckTienTe")){
                String matte = request.getParameter("matte");
                if(TienTe_Model.CheckTienTe(matte, langid)){
                    out.print("success");
                }
                else{
                    out.print("error");
                }
            }
            else if (action.equals("UpdateStatusTT")) {
                String matte = request.getParameter("matte");
                String status = request.getParameter("status");
                String item = request.getParameter("item");
                TienTe_Model.UpdateStatusTT(matte, status);
                NhanVien nv = new NhanVien();
                nv = (NhanVien) session.getAttribute("nv");
                NhatKyHT_Model.GhiNhatKy(3, nv.getMaNV(), "statuscurrency");
                TienTe_Model ttm = new TienTe_Model();
                Vector v = new Vector();
                v = ttm.LayDSTT(langid); // Lấy danh sách khu vực
                request.setAttribute("DSTT", v);
                request.setAttribute("item", item);
                RequestDispatcher rd = request.getRequestDispatcher("admin/listcurrency.jsp");
                rd.forward(request, response);
            }
        } finally { 
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
